package com.hg.library.widget;

import android.content.Context;
import android.view.animation.Interpolator;
import android.widget.OverScroller;

import java.lang.reflect.Field;

/**
 * Created by ASUS-PC on 2016/6/10.
 */
public class HiScroller extends OverScroller {
    private static final String TAG = "HiScroller";
    private int duration = 250;
    private Member member;

    private boolean scrollEnable = true;

    public HiScroller(Context context) {
        super(context);
    }

    public HiScroller(Context context, Interpolator interpolator) {
        super(context, interpolator);
    }

    public HiScroller(Context context, Interpolator interpolator, float bounceCoefficientX, float bounceCoefficientY) {
        super(context, interpolator, bounceCoefficientX, bounceCoefficientY);
    }

    public HiScroller(Context context, Interpolator interpolator, float bounceCoefficientX, float bounceCoefficientY, boolean flywheel) {
        super(context, interpolator, bounceCoefficientX, bounceCoefficientY, flywheel);
    }

    public void setMember(Member member) {
        this.member = member;
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy, int duration) {
        if (scrollEnable)
            super.startScroll(startX, startY, member.scrollX(startX, dx), dy, member.scrollDuration(this.duration));
    }

    public void setScrollDuration(int duration) {
        this.duration = duration;
    }

    public void setInterpolator(Interpolator interpolator) {
        Class aClass = getClass().getSuperclass();
        try {
            Field field = aClass.getDeclaredField("mInterpolator");
            field.setAccessible(true);
            field.set(this, interpolator);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public boolean isScrollEnable() {
        return scrollEnable;
    }

    public void setScrollEnable(boolean scrollEnable) {
        this.scrollEnable = scrollEnable;
    }
}
